// pages/zhengshu/zhengshu.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		pID: 20, //左侧点击到   一级分类
		indexL: 25, //左侧点击到的index  二级分类
		leftList: [],
		leftList6: [],
		rightList6: [],
		leftHight6: 0,
		rightHight6: 0,

		start: 0,
		page: 1,
		size: 6,
		allNum: 0,
		isEnd: false
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {

		this.secondClass();
		this.proNum(this.data.pID, this.data.indexL);

		this.getData(this.data.leftHight6,
			this.data.rightHight6,
			this.data.leftList6,
			this.data.rightList6,
			this.data.start,
			this.data.pID,
			this.data.indexL);
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {


		//获取更多
		this.data.start = this.data.page * this.data.size;
		this.getData(this.data.leftHight6,
			this.data.rightHight6,
			this.data.leftList6,
			this.data.rightList6,
			this.data.start,
			this.data.pID,
			this.data.indexL);

		this.data.page += 1;
		// console.log('-------')
		//  console.log(this.data.allNum);
		//  console.log(this.data.page)
		//  console.log(this.data.size)

		if (this.data.allNum < this.data.page * this.data.size) {
			this.setData({
				isEnd: true
			})
		}
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	//产品数目
	proNum: function(cid, scid) {
		var that = this;
		var url_ = getApp().globalData.url_;
		// console.log(cid)
		// console.log(scid)
		wx.request({
			url: url_ + '/api/product/total?cid=' + cid + '&scid=' + scid,
			method: "GET",
			dataType: "json",
			success: function(res) {
				that.setData({
					allNum: res.data.data.total,
				})
			},
			fail: function(e) {
				console.log("请求失败");
			},
		})
	},
	//点击左侧
	clickLeft: function(e) {
		if (this.data.indexL == e.currentTarget.dataset.id) {
			return;
		}
		this.setData({
			indexL: e.currentTarget.dataset.id,
			pID: e.currentTarget.dataset.pid,
			leftHight6: 0,
			rightHight6: 0,
			leftList6: [],
			rightList6: [],
			start: 0,
			page: 1,
			size: 6,
			isEnd: false
		})
		this.getData(
			this.data.leftHight6,
			this.data.rightHight6,
			this.data.leftList6,
			this.data.rightList6,
			this.data.start,
			this.data.pID,
			this.data.indexL);

	},
	getData: function(lastLH, lastRH, lastLeft, lastRight, start, cid, scid) {
		// console.log(that.data.size)
		var that = this;
		var url_ = getApp().globalData.url_;
		// var _this=this;
		if (this.data.isEnd) {
			return
		}

		wx.request({
			url: url_ + '/api/product?start=' + start + '&count=' + that.data.size + '&cid=' + cid +
				'&scid=' + scid,
			method: "GET",
			dataType: "json",
			success: function(res) {
				//畅销商品
				var allData6 = res.data.data;
				//定义两个临时的变量来记录左右两栏的高度，避免频繁调用setData方法

				var leftH6 = lastLH;
				var rightH6 = lastRH;

				var leftData6 = [];
				var rightData6 = [];
				var CoverWidth6 = 0;
				var CoverHeight6 = 0;
				// console.log(allData6)
				for (let j = 0; j < allData6.length; j++) {
					var img = url_ + allData6[j].cover_image;
					allData6[j].cover_image = img;
					CoverWidth6 = parseInt(allData6[j].imagewidth);
					CoverHeight6 = parseInt(allData6[j].imageheight);
					var currentItemHeight6 = parseInt((CoverHeight6 * 375) / CoverWidth6);
					if (leftH6 == rightH6 || leftH6 <
						rightH6) { //判断左右两侧当前的累计高度，来确定item应该放置在左边还是右边
						leftData6.push(allData6[j]);
						leftH6 += currentItemHeight6;
					} else {
						rightData6.push(allData6[j]);
						rightH6 += currentItemHeight6;
					}
				}
				//新旧数据合并
				var lastLeftData = lastLeft;
				var lastRightData = lastRight;
				lastLeftData.push(...leftData6)
				lastRightData.push(...rightData6)
				//畅销商品
				that.setData({
					// productList: res.data.data,
					leftHight6: leftH6,
					rightHight6: rightH6,
					leftList6: lastLeftData,
					rightList6: lastRightData
				})
			},
			fail: function(e) {
				console.log("请求失败");
			},
		})


	},
	//产品中心左侧二级分类
	secondClass: function() {
		var that = this;
		var url_ = getApp().globalData.url_;
		wx.request({
			url: url_ + '/api/product/category',
			method: "GET",
			dataType: "json",
			success: function(res) {
				// console.log(res.data.data.all);
				that.setData({
					leftList: res.data.data.all,
				})
			},
			fail: function(e) {
				console.log("请求失败");
			},
		})
	},
	pro2GoDetai: function(e) {
		console.log(e.currentTarget.dataset.id)
		wx.navigateTo({
			url: "../product/productDetails?id=" + e.currentTarget.dataset.id
		})
	}





})
